﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using devtm.Editor.TypeSelector;
using Mono.Cecil;

namespace devtm.AutoMapper.CustomCode.TemplateAdd
{
    public partial class AddMapRoot : Form
    {

        CustomTypeRepository repository;


        public AddMapRoot()
        {

            InitializeComponent();

        }



        internal void SetList(MappingModel mappingModel, CustomTypeRepository repository)
        {

            this.repository = repository;
            this.mappingModel = mappingModel;

            if (repository.ContainingProject != null)
            {

                if (!string.IsNullOrEmpty(_source))
                {
                    labelSource.Tag = repository.GetType(_source);
                    if (labelSource.Tag != null)
                        labelSource.Text = (labelSource.Tag as TypeDefinition).FullName;
                }

                if (!string.IsNullOrEmpty(_target))
                {
                    LabelTarget.Tag = repository.GetType(_target);
                    if (LabelTarget.Tag != null)
                        LabelTarget.Text = (LabelTarget.Tag as TypeDefinition).FullName;
                }

            }

        }
       

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
       

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        public TypeDefinition SourceType { get { return (TypeDefinition)labelSource.Tag; } }
        public TypeDefinition TargetType { get { return (TypeDefinition)LabelTarget.Tag; } }


        public bool CreateBack { get { return GenerateBack.Checked; } }

        private static string _source = string.Empty;
        private static string _target = string.Empty;


        private void AddMapRoot_Load(object sender, EventArgs e)
        {
            ComboMatchProperty.SelectedIndex = 0;
        }

    

        private void button3_Click(object sender, EventArgs e)
        {

            using (WindowsSelectType frm = CreateForm(labelSource))
            {
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    TypeDefinition t1 = frm.SelectedItem as TypeDefinition;
                    labelSource.Tag = t1;
                    labelSource.Text = t1.FullName;
                    _source = t1.AssemblyQualifiedName;
                }
            }

        }

 
        private void button4_Click(object sender, EventArgs e)
        {

            using (WindowsSelectType frm = CreateForm(LabelTarget))
            {
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    TypeDefinition t1 = frm.SelectedItem as TypeDefinition;
                    LabelTarget.Tag = t1;
                    LabelTarget.Text = t1.FullName;
                    _target = t1.AssemblyQualifiedName;
                }
            }

        }


        private WindowsSelectType CreateForm(Label label)
        {

            string value = string.Empty;
            if (label.Tag != null)
            {
                TypeDefinition s = label.Tag as TypeDefinition;
                if (s != null)
                    value = s.AssemblyQualifiedName;
            }

            Predicate<TypeDefinition> predicate;
            WindowsSelectType frm = new WindowsSelectType();

            HashSet<string> namespaces = mappingModel.GetNamespaces;

            predicate = null;

            if (DontshowAbstractType.Checked)
                predicate = (c) => c != null & c.IsClass & c.IsPublic & !c.IsAbstract && !c.IsInterface & c.GenericParameters.Count == 0;
            else
                predicate = (c) => c != null & c.IsPublic & c.GenericParameters.Count == 0;

            frm.SetList(repository, predicate, value, true);

            return frm;

        }

       
        private void labelSource_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = (SourceType != null && TargetType != null);
        }

        private void LabelTarget_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = (SourceType != null && TargetType != null);
        }

        private MappingModel mappingModel;

        private void Form_Activated(object sender, EventArgs e)
        {

        }

    }
}
